/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Area_banco;
import br.com.evl.tecinfo.modelo.Area;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tecinfo
 */
public class TelaCadastroArea extends javax.swing.JFrame {

    public TelaGeral telaGeral;
    int editar = 0;

    private Area novaArea;
    private Area_banco areaBD;

    public TelaCadastroArea() {

        initComponents();

        novaArea = new Area();
        areaBD = new Area_banco();

        atualizarTabela();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        JT_Table = new javax.swing.JTable();
        JTF_nome = new javax.swing.JTextField();
        JTF_abreviacao = new javax.swing.JTextField();
        jB_remover = new javax.swing.JButton();
        jB_editar1 = new javax.swing.JButton();
        jBcadastrar_tc1 = new javax.swing.JButton();
        JB_voltar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jB_cadastrarArea = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jB_BackgroundArea = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar área");
        setMaximumSize(new java.awt.Dimension(600, 600));
        setMinimumSize(new java.awt.Dimension(600, 600));
        setResizable(false);
        getContentPane().setLayout(null);

        JT_Table.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JT_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Abreviação"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(JT_Table);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(40, 270, 510, 260);

        JTF_nome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_nome.setText("nome");
        getContentPane().add(JTF_nome);
        JTF_nome.setBounds(150, 130, 380, 30);

        JTF_abreviacao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_abreviacao.setText("abr");
        getContentPane().add(JTF_abreviacao);
        JTF_abreviacao.setBounds(150, 170, 380, 30);

        jB_remover.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_excluirv2.png"))); // NOI18N
        jB_remover.setText("Remover");
        jB_remover.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_removerActionPerformed(evt);
            }
        });
        getContentPane().add(jB_remover);
        jB_remover.setBounds(370, 550, 120, 40);

        jB_editar1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_editar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icon_editar1v2.png"))); // NOI18N
        jB_editar1.setText("Editar");
        jB_editar1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_editar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_editar1ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_editar1);
        jB_editar1.setBounds(240, 550, 120, 40);

        jBcadastrar_tc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBcadastrar_tc1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_add1v2.png"))); // NOI18N
        jBcadastrar_tc1.setText("Cadastrar");
        jBcadastrar_tc1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jBcadastrar_tc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcadastrar_tc1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBcadastrar_tc1);
        jBcadastrar_tc1.setBounds(110, 550, 120, 40);

        JB_voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/go-back.png"))); // NOI18N
        JB_voltar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        JB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(JB_voltar);
        JB_voltar.setBounds(540, 10, 50, 40);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logomarcav2.png"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 0, 140, 110);

        jB_cadastrarArea.setFont(new java.awt.Font("Trebuchet MS", 1, 30)); // NOI18N
        jB_cadastrarArea.setText("Cadastrar Área");
        getContentPane().add(jB_cadastrarArea);
        jB_cadastrarArea.setBounds(220, 40, 220, 36);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Abreviação:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(50, 180, 80, 15);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Nome:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(50, 140, 80, 15);

        jLabel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel4);
        jLabel4.setBounds(34, 100, 520, 160);

        jB_BackgroundArea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/back_azul.jpg"))); // NOI18N
        jB_BackgroundArea.setCursor(new java.awt.Cursor(java.awt.Cursor.NE_RESIZE_CURSOR));
        jB_BackgroundArea.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jB_BackgroundArea.setMaximumSize(new java.awt.Dimension(600, 600));
        jB_BackgroundArea.setMinimumSize(new java.awt.Dimension(600, 600));
        getContentPane().add(jB_BackgroundArea);
        jB_BackgroundArea.setBounds(0, 0, 600, 600);

        setSize(new java.awt.Dimension(616, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jB_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_removerActionPerformed

        Area removeArea = new Area();

        removeArea.setAbreviacao((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 1));

        areaBD.remover(removeArea);

        atualizarTabela();


    }//GEN-LAST:event_jB_removerActionPerformed

    private void jB_editar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_editar1ActionPerformed

        if (editar == 0) {

            JTF_nome.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));
            JTF_abreviacao.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 1));
            jB_editar1.setText("Concluir");
            jB_remover.setEnabled(false);
            jBcadastrar_tc1.setEnabled(false);
            JT_Table.setEnabled(false);

            editar = 1;

        } else {

            jB_removerActionPerformed(null);
            jBcadastrar_tc1ActionPerformed(null);

            jB_editar1.setText("Editar");
            jB_remover.setEnabled(true);
            jBcadastrar_tc1.setEnabled(true);
            JT_Table.setEnabled(true);

            editar = 0;

        }

    }//GEN-LAST:event_jB_editar1ActionPerformed

    private void jBcadastrar_tc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcadastrar_tc1ActionPerformed

        novaArea.setNome(JTF_nome.getText());
        novaArea.setAbreviacao(JTF_abreviacao.getText());

        areaBD.inserir(novaArea);

        atualizarTabela();

        /*if (cadastrar_editar == -1) {
         DefaultTableModel dtm = (DefaultTableModel) JT_Table.getModel();
         Object[] opcoes = {JTF_nome.getText(), JTF_abreviacao.getText(),
         JTF_cod.getText()};
         dtm.addRow(opcoes);
         JTF_nome.setText("");
         JTF_abreviacao.setText("");
         JTF_cod.setText("");
            
         } else {
         int atualizar = JT_Table.getSelectedRow();
         JT_Table.setValueAt(JTF_nome.getText(), atualizar, 0);
         JT_Table.setValueAt(JTF_abreviacao.getText(), atualizar, 1);
         JT_Table.setValueAt(JTF_cod.getText(), atualizar, 2);
            
         JOptionPane.showMessageDialog(null, "Dados atualizados com sucesso!");
         cadastrar_editar = -1;
         JTF_nome.setText("");
         JTF_abreviacao.setText("");
         JTF_cod.setText("");
            
         }*/

    }//GEN-LAST:event_jBcadastrar_tc1ActionPerformed

    private void JB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_voltarActionPerformed

        this.setVisible(false);

    }//GEN-LAST:event_JB_voltarActionPerformed

    private void atualizarTabela() {

        areaBD = new Area_banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Nome", "Abreviação"});

        try {
            while (areaBD.consulta.next()) {

                Object[] opcoes = {areaBD.consulta.getString("nome"), areaBD.consulta.getString("abreviacao")};
                dtm.addRow(opcoes);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        JT_Table.setModel(dtm);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_voltar;
    private javax.swing.JTextField JTF_abreviacao;
    private javax.swing.JTextField JTF_nome;
    private javax.swing.JTable JT_Table;
    private javax.swing.JLabel jB_BackgroundArea;
    private javax.swing.JLabel jB_cadastrarArea;
    private javax.swing.JButton jB_editar1;
    private javax.swing.JButton jB_remover;
    private javax.swing.JButton jBcadastrar_tc1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
